// components/order/preview/preview.js
const account = wx.getStorageSync('userInfo').account
const db = wx.cloud.database()

Component({
  /**
   * 组件的属性列表
   */
  properties: {
    orderInfo:Object
  },

  /**
   * 组件的初始数据
   */
  data: {

  },

  /**
   * 组件的方法列表
   */
  methods: {
    click(){
      let {btntxt} = this.data

      if(btntxt == '立即付款'){
        this.pay()
      }else if(btntxt == '联系商家'){
        this.call()
      }else if(btntxt == '确认收货'){
        this.confirm()
      }
    },

    //立即付款
    pay(){
      let {payment} = this.data.orderInfo
      wx.showModal({
        content:'您将支付'+payment+'元，确认支付？'
      }).then(res=>{
        if(res.confirm){
          //判断用户余额是否足够
          db.collection('userList').where({
            account,
          }).get().then(res=>{
            let balance = res.data[0].balance
            if(balance - payment < 0){
              //余额不足，生成订单，但订单待付款
              wx.showModal({
                content:'余额不足，是否充值？'
              }).then(res=>{
                if(res.confirm){
                  wx.navigateTo({
                    url: '/pages/wallet/wallet',
                  })
                }
              })
            }else{
              //余额够，生成订单，订单状态：待发货
              //从用户余额中扣除响应金额
              db.collection('userList').where({
                account,
              }).get().then(res=>{
                let {_id,balance} = res.data[0]
                balance = parseFloat((balance - parseFloat(payment)).toFixed(2))
                db.collection('userList').doc(_id).update({
                  data:{
                    balance,
                  }
                }).then(res=>{
                  wx.showToast({
                    title: '支付成功',
                    icon:'success'
                  })

                  let {_id} = this.data.orderInfo
                  db.collection('orders').doc(_id).update({
                    data:{
                      state:'待配送'
                    }
                  }).then(()=>{
                    this.fresh()
                  })

                }).catch(error=>{
                  console.log(error)
                  wx.showToast({
                    title: '支付失败',
                    icon:'error'
                  })
                })
              })
            }
          }).catch(error=>{
            console.log(error)
          })
        }
    })
    },
    
    //联系商家
    call(){
      wx.makePhoneCall({
        phoneNumber: this.data.orderInfo.products[0].phoneNumber,
      })
    },

    //确认收货
    confirm(){
      let {_id} = this.data.orderInfo

      db.collection('orders').doc(_id).update({
        data:{
          state:'交易成功'
        }
      }).then(()=>{
        this.fresh()
      })
    },

    //更多订单相关信息
    moreInfo(){
      wx.navigateTo({
        url: '/pages/orderDetail/orderDetail?orderInfo='+JSON.stringify(this.data.orderInfo),
      })
    },

    //刷新当前页面
    fresh(){
      var page = getCurrentPages().pop()
      page.init()
    }
  },

  attached(){
    this.setData({
      orderInfo:this.properties.orderInfo
    })

    let state = this.properties.orderInfo.state
    if(state == '待付款'){
      this.setData({
        state:'买家待付款',
        btntxt:'立即付款'
      })
    }else if(state == '待配送'){
      this.setData({
        state:'卖家已接单',
        btntxt:'联系商家'
      })
    }else if(state == '待收货'){
      this.setData({
        state:'卖家已发货',
        btntxt:'确认收货'
      })
    }else{
      this.setData({
        state:'交易成功',
        btntxt:''
      })
    }
  }
})
